//package medicalApp.velociraptors.com;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

/**
 * @author Vishnu
 *
 */
public class LookupTableModel extends AbstractTableModel {
	private Object[][] data;
	private String columnNames[] = {"First Name", "Last Name", "Date of Birth", "Social Security Number"};
	private MedAppController mac;
	private Connection con;
	ArrayList<String> patientFirstNames;
	ArrayList<String> patientLastNames;
	ArrayList<String> patientDOB;
	ArrayList<Integer> patientSSN;
	private int socialSN;
	private String dateOfBirth;
	private String lastName;
	private String firstName;
	private boolean boolFirstName;
	private boolean boolLastName;
	private boolean boolDOB;
	private boolean boolSSN;

	public LookupTableModel(final MedAppController mac, String firstName2, String lastName2, String dateOfBirth2, int socialSN2){
		firstName = firstName2;
		mac.out.println("Lookup First Name: "+ firstName2);
		lastName = lastName2;
		mac.out.println("Lookup Last Name: "+ lastName2);
		dateOfBirth = dateOfBirth2;
		mac.out.println("Lookup DOB: "+ dateOfBirth2);
		socialSN = socialSN2;
		mac.out.println("Lookup SSN: "+ socialSN2);

		boolFirstName = !firstName.equals("");
		mac.out.println("First Name not is null: "+ boolFirstName);
		boolLastName = !lastName.equals("");
		mac.out.println("Last Name not is null: "+ boolLastName);
		boolDOB = !dateOfBirth.equals("");
		mac.out.println("dob not is null: "+ boolDOB);
		boolSSN = socialSN2!=0;
		mac.out.println("SSN not is null: "+ boolSSN);

		this.mac = mac;
		con = mac.getCon();
		patientFirstNames = new ArrayList<String>();
		patientLastNames = new ArrayList<String>();
		patientDOB = new ArrayList<String>();
		patientSSN = new ArrayList<Integer>();		
		
		String sql="";
		if(boolFirstName&&boolLastName&&boolDOB&&boolSSN){
			sql = "SELECT * FROM `patients` WHERE `P_FIRSTNAME` ='"+firstName+"' AND `P_LASTNAME`= '"+lastName+"'AND `DOB` ='"+dateOfBirth+"' AND `P_SSN`= '"+socialSN+"'";
		}
		else if((!boolFirstName)&&boolLastName&&boolDOB&&boolSSN){
			sql = "SELECT * FROM `patients` WHERE `P_LASTNAME`= '"+lastName+"'AND `DOB` ='"+dateOfBirth+"' AND `P_SSN`= '"+socialSN+"'";
		}
		else if(boolFirstName&&!boolLastName&&boolDOB&&boolSSN){
			sql = "SELECT * FROM `patients` WHERE `P_FIRSTNAME` ='"+firstName+"' AND `DOB` ='"+dateOfBirth+"' AND `P_SSN`= '"+socialSN+"'";
		}
		else if(boolFirstName&&boolLastName&&!boolDOB&&boolSSN){
			sql = "SELECT * FROM `patients` WHERE `P_FIRSTNAME` ='"+firstName+"' AND `P_LASTNAME`= '"+lastName+"'AND `P_SSN`= '"+socialSN+"'";
		}
		else if(boolFirstName&&boolLastName&&boolDOB&&!boolSSN){
			sql = "SELECT * FROM `patients` WHERE `P_FIRSTNAME` ='"+firstName+"' AND `P_LASTNAME`= '"+lastName+"'AND `DOB` ='"+dateOfBirth+"'";
		}
		else if(!boolFirstName&&!boolLastName&&boolDOB&&boolSSN){
			sql = "SELECT * FROM `patients` WHERE `DOB` ='"+dateOfBirth+"' AND `P_SSN`= '"+socialSN+"'";
		}
		else if(boolFirstName&&!boolLastName&&!boolDOB&&boolSSN){
			sql = "SELECT * FROM `patients` WHERE `P_FIRSTNAME` ='"+firstName+"' AND `P_SSN`= '"+socialSN+"'";
		}
		else if(boolFirstName&&boolLastName&&!boolDOB&&!boolSSN){
			sql = "SELECT * FROM `patients` WHERE `P_FIRSTNAME` ='"+firstName+"' AND `P_LASTNAME`= '"+lastName+"'";
		}
		else if(boolFirstName&&!boolLastName&&boolDOB&&!boolSSN){
			sql = "SELECT * FROM `patients` WHERE `P_FIRSTNAME` ='"+firstName+"' AND `DOB` ='"+dateOfBirth+"' AND ";
		}
		else if(!boolFirstName&&boolLastName&&!boolDOB&&boolSSN){
			sql = "SELECT * FROM `patients` WHERE `P_LASTNAME`= '"+lastName+"' AND `P_SSN`= '"+socialSN+"'";
		}
		else if(!boolFirstName&&!boolLastName&&!boolDOB&&boolSSN){
			sql = "SELECT * FROM `patients` WHERE `P_SSN`= '"+socialSN+"'";
		}
		else if(boolFirstName&&!boolLastName&&!boolDOB&&!boolSSN){
			sql = "SELECT * FROM `patients` WHERE `P_FIRSTNAME` ='"+firstName+"'";
		}
		else if(!boolFirstName&&boolLastName&&!boolDOB&&!boolSSN){
			sql = "SELECT * FROM `patients` WHERE `P_LASTNAME`= '"+lastName+"'";
			
		}
		else if(!boolFirstName&&!boolLastName&&!boolDOB&&boolSSN){
			sql = "SELECT * FROM `patients` WHERE `DOB` ='"+dateOfBirth+"'";
		}
		else if(!boolFirstName&&!boolLastName&&!boolDOB&&boolSSN){
			sql = "SELECT * FROM `patients` WHERE `P_SSN`= '"+socialSN+"'";
		}
		else if(!(boolFirstName&&boolLastName&&boolDOB&&boolSSN)){
			sql = "SELECT * FROM `patients`";
		}

		try {
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while(rs.next()){
				patientFirstNames.add(rs.getString("P_FIRSTNAME"));
				patientLastNames.add(rs.getString("P_LASTNAME"));
				patientDOB.add(rs.getString("DOB"));
				patientSSN.add(rs.getInt("P_SSN"));
			}



		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		data = new Object[patientFirstNames.size()][4];

		for(int i = 0; i<patientFirstNames.size();i++){
			data[i][0] = patientFirstNames.get(i);
		}

		for(int i = 0; i<patientLastNames.size();i++){
			data[i][1] = patientLastNames.get(i);
		}

		for(int i = 0; i<patientDOB.size();i++){
			data[i][2] = patientDOB.get(i);
		}

		for(int i = 0; i<patientSSN.size();i++){
			data[i][3] = patientSSN.get(i);
		}
	}
	
	private int minimum(int a, int b, int c, int d){
		int min = Math.min(Math.min(a,b), Math.min(c,d));
		return min;
	}
	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return data.length;
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex){
		// TODO Auto-generated method stub
		//		if(data[rowIndex][columnIndex]==null){
		//			return null;
		//		}
		return data[rowIndex][columnIndex];
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}
}
